package com.gamatechno.solodestinationnew;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.gamatechno.solodestinationnew.utils.Utils;
import com.gamatechno.solodestinationnew.utils.Webservice;
import com.nostra13.universalimageloader.core.ImageLoader;

public class PhotoReadyActivity extends BaseActivity {
	Bitmap bmp;
	View mView;
	TextView txtTemp, Status, day;
	ImageView thumb;
	ImageView ready;
	Button btnShare;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_photo_ready);
		mView = findViewById(R.id.view_ready);
		txtTemp = (TextView) mView.findViewById(R.id.temp_forecast);
		Status = (TextView) mView.findViewById(R.id.status_forecast);
		thumb = (ImageView) mView.findViewById(R.id.thumb_forecast);
		ready = (ImageView) mView.findViewById(R.id.ready);
		btnShare = (Button) findViewById(R.id.btnshare);
		day = (TextView) mView.findViewById(R.id.day);
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat p_day = new SimpleDateFormat("dd MMMM yyyy");
		String t_day = p_day.format(cal.getTime());

		day.setText(t_day);

		// setting view include
		mView.setDrawingCacheEnabled(true);
		mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
				MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());
		mView.buildDrawingCache(true);

		byte[] byteArray = getIntent().getByteArrayExtra("image");
		Options options = new BitmapFactory.Options();
		options.inScaled = false;
		options.inDither = false;
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
		BitmapDrawable ob = new BitmapDrawable(bmp);
		ready.setBackgroundDrawable(ob);
		getCurrentWeather(Webservice.GET_WEATHER);

		btnShare.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bitmap b = Bitmap.createBitmap(mView.getDrawingCache());
				mView.setDrawingCacheEnabled(false);
				String pathofBmp = Images.Media.insertImage(
						getContentResolver(), b, "SoloDestinationImage", null);
				Uri bmpUri = Uri.parse(pathofBmp);
				final Intent emailIntent1 = new Intent(
						android.content.Intent.ACTION_SEND);
				emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				emailIntent1.putExtra(Intent.EXTRA_STREAM, bmpUri);
				emailIntent1.setType("image/jpeg");
				startActivity(emailIntent1);
			}
		});
	}

	private void getCurrentWeather(String URL) {
		JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
				URL, null, new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						try {
							JSONObject obj = response.getJSONObject("main");
							JSONArray arr = response.getJSONArray("weather");
							JSONObject objW = arr.getJSONObject(0);
							if (obj.has("temp"))
								txtTemp.setText("" + obj.getInt("temp") + " "
										+ (char) 0x00B0 + "C");
							Status.setText("" + objW.getString("main"));
							ImageLoader.getInstance().displayImage(
									Webservice.GET_ICON_PATH_WEATHER(objW
											.getString("icon")), thumb,
									BaseApplication.getDisplayImageOptions());

						} catch (JSONException e) {
							Utils.TRACE(
									"MainActivity.getCurrentWeather(...).new Listener() {...}",
									"onResponse : " + e.getMessage());
						}
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						txtTemp.setText("Failed");
						Utils.TRACE(
								"MainActivity.getCurrentWeather(...).new ErrorListener() {...}",
								"getCurrentWeather : " + error);
						Utils.showVolleyError(PhotoReadyActivity.this, error);
					}
				});
		txtTemp.setText("Loading...");
		BaseApplication.getInstance().addToRequestQueue(request,
				"PhotoReadyActivity.java");
	}

}
